package com.financing.wap.web.controller.api;

import java.math.BigDecimal;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.financing.wap.common.ResultMap;
import com.financing.wap.common.ResultMap.STATUS;
import com.financing.wap.common.Session;
import com.financing.wap.common.SessionUtil;
import com.financing.wap.dto.AccountWithdrawDTO;
import com.financing.wap.dto.BankCardDTO;
import com.financing.wap.dto.UserDTO;
import com.financing.wap.service.IAccountWithdrawService;
import com.financing.wap.service.IBankCardService;
import com.financing.wap.util.Constants;
import com.financing.wap.util.DTOdateSetUtil;
import com.financing.wap.util.StringUtil;
import com.financing.wap.web.controller.BaseController;

/**
 * 账户提现-Controller
 * 
 * @author lisu
 *
 */
@Controller("accountWithdrawController")
@RequestMapping("/api/accountWithdraw")
public class AccountWithdrawController extends BaseController {

	private Logger logger = Logger.getLogger(getClass());

	@Autowired
	private IAccountWithdrawService accountWithdrawServiceImpl;
	
	@Autowired
	private IBankCardService bankCardServiceImpl;

	/**
	 * 个人账户提现历史列表
	 * @param accountWithdrawDTO
	 * @return
	 */
	@RequestMapping("/list")
	@ResponseBody
	public ResultMap queryList(AccountWithdrawDTO accountWithdrawDTO) {
		ResultMap result = new ResultMap();

		logger.info("In AccountWithdrawController queryList Begin");

		Session session = SessionUtil.getSession();
		UserDTO loginUser = session.getUser();
		accountWithdrawDTO.setUserId(loginUser.getId());
		try {
			List<AccountWithdrawDTO> list = accountWithdrawServiceImpl
					.queryAccountWithdrawList(accountWithdrawDTO);
			
			for (int i = 0; i < list.size(); i++) {
				AccountWithdrawDTO bean = 	list.get(i);
				if(bean == null){
					continue;
				}
				bean.setCardNumber(StringUtil.encryptCardNumber(bean.getCardNumber()));
				bean.setUserName(StringUtil.encryptUserName(bean.getUserName()));
			}
			result.setAttribute("list", list);
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			result.setMessage("个人提现查询列表成功!");
		} catch (Exception e) {
			logger.error("error AccountWithdrawController queryList:", e);
			result.setStatus(STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
			result.setMessage("个人提现查询列表失败");
		}
		logger.info("In AccountWithdrawController queryList end");
		return result;
	}

	/**
	 * 申请提现操作
	 * @param accountWithdrawDTO
	 * @return
	 */
	@RequestMapping("/add")
	@ResponseBody
	public ResultMap addAccountWithdraw(AccountWithdrawDTO accountWithdrawDTO) {
		ResultMap result = new ResultMap();

		//如果是现金方式-自己调用方法并且返回
		if(Constants.BANK_CARD_TYPE_CASH.equals(accountWithdrawDTO.getBankType())){
			return addAccountWithdrawByCash(accountWithdrawDTO);
		}
		logger.info("In AccountWithdrawController addAccountWithdraw Begin");


		// 添加参数验证  提现金额,银行卡管理id
		if (accountWithdrawDTO.getWithdrawAmount() == null
				|| accountWithdrawDTO.getBankCardId() == null || !(accountWithdrawDTO.getWithdrawAmount().compareTo(new BigDecimal(1)) >= 0)) {
			result.setStatus(STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
			result.setMessage("参数缺失");
			return result;
		}
		
		logger.info("In AccountWithdrawController addAccountWithdraw check parameters success");
		
		// 初始化属性
		DTOdateSetUtil.setCreateInfo(accountWithdrawDTO);
		DTOdateSetUtil.setUpdateInfo(accountWithdrawDTO);
		Session session = SessionUtil.getSession();
		UserDTO loginUser = session.getUser();
		accountWithdrawDTO.setUserId(loginUser.getId());
		accountWithdrawDTO.setAccountId(loginUser.getAccountId());
		
		//根据 银行卡管理id-获取详细
		BankCardDTO bankCardDTO = new BankCardDTO();
		bankCardDTO.setUserId(loginUser.getId());
		bankCardDTO.setId(accountWithdrawDTO.getBankCardId());
		try {
			BankCardDTO bankCardDTOview = bankCardServiceImpl.queryBankCard(bankCardDTO);
			
			//判断信息
			if(bankCardDTOview == null || StringUtil.isNullOrEmptyStr(bankCardDTOview.getBankType()) || StringUtil.isNullOrEmptyStr(bankCardDTOview.getCardNumber())){
				result.setStatus(STATUS.ERROR);
				result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
				return result;
			}
			//不是支付宝  就要填写 账户名
			if(!Constants.BANK_CARD_TYPE_ZFB.equals(bankCardDTOview.getBankType()) && StringUtil.isNullOrEmptyStr(bankCardDTOview.getUserName())){
				result.setStatus(STATUS.ERROR);
				result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
				return result;
			}
			if (bankCardServiceImpl.checkBankName(bankCardDTOview)) {
				result.setStatus(STATUS.ERROR);
				result.setStatusCode(ResultMap.STATUS_CODE_CARD_UNMBER_NOTONLY);
				result.setMessage("提现账户不允许重复");
				return result;
			}
			logger.info("In AccountWithdrawController addAccountWithdraw check data true success");
			
			//验证完-赋值信息
			accountWithdrawDTO.setBankType(bankCardDTOview.getBankType());
			accountWithdrawDTO.setUserName(bankCardDTOview.getUserName());
			accountWithdrawDTO.setCardNumber(bankCardDTOview.getCardNumber());
			accountWithdrawDTO.setWithdrawFlowId(StringUtil.getWithdrawFlowId());
			
			String resultCode = accountWithdrawServiceImpl.insertAccountWithdraw(accountWithdrawDTO);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			result.setStatusCode(resultCode);
		} catch (Exception e) {
			logger.error("error AccountWithdrawController addAccountWithdraw:", e);
			result.setStatus(STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		logger.info("In AccountWithdrawController addAccountWithdraw end");
		return result;
	}

	@RequestMapping("/update")
	@ResponseBody
	public ResultMap updateBankCard(BankCardDTO bankCardDTO) {
		ResultMap result = new ResultMap();

		logger.info("In BankCardController deleteBankCard Begin");

		// 初始化属性
		DTOdateSetUtil.setUpdateInfo(bankCardDTO);
		Session session = SessionUtil.getSession();
		UserDTO loginUser = session.getUser();
		bankCardDTO.setUserId(loginUser.getId());
		try {
			// TODO
			int influenceCount = 0;
			logger.info("In BankCardController deleteBankCard count=:"
					+ influenceCount);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
		} catch (Exception e) {
			logger.error("error BankCardController deleteBankCard:", e);
			result.setStatus(STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		logger.info("In BankCardController deleteBankCard end");
		return result;
	}
	
	/**
	 * 提现 -现金方式
	 * @param accountWithdrawDTO
	 * @return
	 */
	public ResultMap addAccountWithdrawByCash(AccountWithdrawDTO accountWithdrawDTO) {
		ResultMap result = new ResultMap();

		logger.info("In BankCardController addAccountWithdrawByCash Begin");

		// 初始化属性
		DTOdateSetUtil.setCreateInfo(accountWithdrawDTO);
		DTOdateSetUtil.setUpdateInfo(accountWithdrawDTO);
		Session session = SessionUtil.getSession();
		UserDTO loginUser = session.getUser();
		accountWithdrawDTO.setUserId(loginUser.getId());
		accountWithdrawDTO.setAccountId(loginUser.getAccountId());
		
		// 添加参数验证  提现金额
		if (accountWithdrawDTO.getWithdrawAmount() == null || !(accountWithdrawDTO.getWithdrawAmount().compareTo(new BigDecimal(500)) >= 0)) {
			result.setStatus(STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
			result.setMessage("参数缺失");
			return result;
		}
		try {
			//验证完-赋值信息
			accountWithdrawDTO.setWithdrawFlowId(StringUtil.getWithdrawFlowId());
			
			String resultCode = accountWithdrawServiceImpl.insertAccountWithdraw(accountWithdrawDTO);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			result.setStatusCode(resultCode);
		} catch (Exception e) {
			logger.error("error BankCardController deleteBankCard:", e);
			result.setStatus(STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		logger.info("In BankCardController deleteBankCard end");
		return result;
	}
}
